home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / ralloc.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  14KB  |  522 lines

  1. /* Block-relocating memory allocator. 
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* NOTES:
  21.  
  22.    Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
  23.    rather than all of them.  This means allowing for a possible
  24.    hole between the first bloc and the end of malloc storage. */
  25.  
  26. #ifdef emacs
  27.  
  28. #include "config.h"
  29. #include "lisp.h"        /* Needed for VALBITS.  */
  30.  
  31. #ifdef STDC_HEADERS
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #endif
  35.  
  36. #undef NULL
  37.  
  38. /* The important properties of this type are that 1) it's a pointer, and
  39.    2) arithmetic on it should work as if the size of the object pointed
  40.    to has a size of 1.  */
  41. #if 0 /* Arithmetic on void* is a GCC extension.  */
  42. #ifdef __STDC__
  43. typedef void *POINTER;
  44. #else
  45.  
  46. #ifdef    HAVE_CONFIG_H
  47. #include "config.h"
  48. #endif
  49.  
  50. typedef char *POINTER;
  51.  
  52. #endif
  53. #endif /* 0 */
  54.  
  55. /* Unconditionally use char * for this.  */
  56. typedef char *POINTER;
  57.  
  58. typedef unsigned long SIZE;
  59.  
  60. /* Declared in dispnew.c, this version doesn't screw up if regions
  61.    overlap.  */
  62. #ifndef STDC_HEADERS
  63. extern void safe_bcopy ();
  64. #else
  65. #define safe_bcopy(f, t, l) memmove(t, f, l)
  66. #endif
  67.  
  68. #include "getpagesize.h"
  69.  
  70. #else    /* Not emacs.  */
  71.  
  72. #include <stddef.h>
  73.  
  74. typedef size_t SIZE;
  75. typedef void *POINTER;
  76.  
  77. #include <unistd.h>
  78. #include <malloc.h>
  79. #include <string.h>
  80.  
  81. #define safe_bcopy(x, y, z) memmove (y, x, z)
  82.  
  83. #endif    /* emacs.  */
  84.  
  85. #define NIL ((POINTER) 0)
  86.  
  87. /* A flag to indicate whether we have initialized ralloc yet.  For
  88.    Emacs's sake, please do not make this local to malloc_init; on some
  89.    machines, the dumping procedure makes all static variables
  90.    read-only.  On these machines, the word static is #defined to be
  91.    the empty string, meaning that r_alloc_initialized becomes an
  92.    automatic variable, and loses its value each time Emacs is started up.  */
  93. static int r_alloc_initialized = 0;
  94.  
  95. static void r_alloc_init ();
  96.  
  97. /* Declarations for working with the malloc, ralloc, and system breaks.  */
  98.  
  99. /* Function to set the real break value. */
  100. static POINTER (*real_morecore) ();
  101.  
  102. /* The break value, as seen by malloc (). */
  103. static POINTER virtual_break_value;
  104.  
  105. /* The break value, viewed by the relocatable blocs. */
  106. static POINTER break_value;
  107.  
  108. /* The REAL (i.e., page aligned) break value of the process. */
  109. static POINTER page_break_value;
  110.  
  111. /* This is the size of a page.  We round memory requests to this boundary.  */
  112. static int page_size;
  113.  
  114. /* Whenever we get memory from the system, get this many extra bytes.  This 
  115.    must be a multiple of page_size.  */
  116. static int extra_bytes;
  117.  
  118. /* Macros for rounding.  Note that rounding to any value is possible
  119.    by changing the definition of PAGE. */
  120. #define PAGE (getpagesize ())
  121. #define ALIGNED(addr) (((unsigned int) (addr) & (page_size - 1)) == 0)
  122. #define ROUNDUP(size) (((unsigned int) (size) + page_size - 1) & ~(page_size - 1))
  123. #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
  124.  
  125. /* Functions to get and return memory from the system.  */
  126.  
  127. /* Obtain SIZE bytes of space.  If enough space is not presently available
  128.    in our process reserve, (i.e., (page_break_value - break_value)),
  129.    this means getting more page-aligned space from the system.
  130.  
  131.    Return non-zero if all went well, or zero if we couldn't allocate
  132.    the memory.  */
  133. static int
  134. obtain (size)
  135.      SIZE size;
  136. {
  137.   SIZE already_available = page_break_value - break_value;
  138.  
  139.   if (already_available < size)
  140.     {
  141.       SIZE get = ROUNDUP (size - already_available);
  142.       /* Get some extra, so we can come here less often.  */
  143.       get += extra_bytes;
  144.  
  145.       if ((*real_morecore) (get) == 0)
  146.     return 0;
  147.  
  148.       page_break_value += get;
  149.     }
  150.  
  151.   break_value += size;
  152.  
  153.   return 1;
  154. }
  155.  
  156. /* Obtain SIZE bytes of space and return a pointer to the new area.
  157.    If we could not allocate the space, return zero.  */
  158.  
  159. static POINTER
  160. get_more_space (size)
  161.      SIZE size;
  162. {
  163.   POINTER ptr = break_value;
  164.   if (obtain (size))
  165.     return ptr;
  166.   else
  167.     return 0;
  168. }
  169.  
  170. /* Note that SIZE bytes of space have been relinquished by the process.
  171.    If SIZE is more than a page, return the space to the system. */
  172.  
  173. static void
  174. relinquish (size)
  175.      SIZE size;
  176. {
  177.   POINTER new_page_break;
  178.   int excess;
  179.  
  180.   break_value -= size;
  181.   new_page_break = (POINTER) ROUNDUP (break_value);
  182.   excess = (char *) page_break_value - (char *) new_page_break;
  183.   
  184.   if (excess > extra_bytes * 2)
  185.     {
  186.       /* Keep extra_bytes worth of empty space.
  187.      And don't free anything unless we can free at least extra_bytes.  */
  188.       if ((*real_morecore) (extra_bytes - excess) == 0)
  189.     abort ();
  190.  
  191.       page_break_value += extra_bytes - excess;
  192.     }
  193.  
  194.   /* Zero the space from the end of the "official" break to the actual
  195.      break, so that bugs show up faster.  */
  196.   bzero (break_value, ((char *) page_break_value - (char *) break_value));
  197. }
  198.  
  199. /* The meat - allocating, freeing, and relocating blocs.  */
  200.  
  201. /* These structures are allocated in the malloc arena.
  202.    The linked list is kept in order of increasing '.data' members.
  203.    The data blocks abut each other; if b->next is non-nil, then
  204.    b->data + b->size == b->next->data.  */
  205. typedef struct bp
  206. {
  207.   struct bp *next;
  208.   struct bp *prev;
  209.   POINTER *variable;
  210.   POINTER data;
  211.   SIZE size;
  212. } *bloc_ptr;
  213.  
  214. #define NIL_BLOC ((bloc_ptr) 0)
  215. #define BLOC_PTR_SIZE (sizeof (struct bp))
  216.  
  217. /* Head and tail of the list of relocatable blocs. */
  218. static bloc_ptr first_bloc, last_bloc;
  219.  
  220. /* Find the bloc referenced by the address in PTR.  Returns a pointer
  221.    to that block. */
  222.  
  223. static bloc_ptr
  224. find_bloc (ptr)
  225.      POINTER *ptr;
  226. {
  227.   register bloc_ptr p = first_bloc;
  228.  
  229.   while (p != NIL_BLOC)
  230.     {
  231.       if (p->variable == ptr && p->data == *ptr)
  232.     return p;
  233.  
  234.       p = p->next;
  235.     }
  236.  
  237.   return p;
  238. }
  239.  
  240. /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
  241.    Returns a pointer to the new bloc, or zero if we couldn't allocate
  242.    memory for the new block.  */
  243.  
  244. static bloc_ptr
  245. get_bloc (size)
  246.      SIZE size;
  247. {
  248.   register bloc_ptr new_bloc;
  249.  
  250.   if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
  251.       || ! (new_bloc->data = get_more_space (size)))
  252.     {
  253.       if (new_bloc)
  254.     free (new_bloc);
  255.  
  256.       return 0;
  257.     }
  258.  
  259.   new_bloc->size = size;
  260.   new_bloc->next = NIL_BLOC;
  261.   new_bloc->variable = (POINTER *) NIL;
  262.  
  263.   if (first_bloc)
  264.     {
  265.       new_bloc->prev = last_bloc;
  266.       last_bloc->next = new_bloc;
  267.       last_bloc = new_bloc;
  268.     }
  269.   else
  270.     {
  271.       first_bloc = last_bloc = new_bloc;
  272.       new_bloc->prev = NIL_BLOC;
  273.     }
  274.  
  275.   return new_bloc;
  276. }
  277.  
  278. /* Relocate all blocs from BLOC on upward in the list to the zone
  279.    indicated by ADDRESS.  Direction of relocation is determined by
  280.    the position of ADDRESS relative to BLOC->data.
  281.  
  282.    If BLOC is NIL_BLOC, nothing is done.
  283.  
  284.    Note that ordering of blocs is not affected by this function. */
  285.  
  286. static void
  287. relocate_some_blocs (bloc, address)
  288.      bloc_ptr bloc;
  289.      POINTER address;
  290. {
  291.   if (bloc != NIL_BLOC)
  292.     {
  293.       register SIZE offset = address - bloc->data;
  294.       register SIZE data_size = 0;
  295.       register bloc_ptr b;
  296.       
  297.       for (b = bloc; b != NIL_BLOC; b = b->next)
  298.     {
  299.       data_size += b->size;
  300.       b->data += offset;
  301.       *b->variable = b->data;
  302.     }
  303.  
  304.       safe_bcopy (address - offset, address, data_size);
  305.     }
  306. }
  307.  
  308.  
  309. /* Free BLOC from the chain of blocs, relocating any blocs above it
  310.    and returning BLOC->size bytes to the free area. */
  311.  
  312. static void
  313. free_bloc (bloc)
  314.      bloc_ptr bloc;
  315. {
  316.   if (bloc == first_bloc && bloc == last_bloc)
  317.     {
  318.       first_bloc = last_bloc = NIL_BLOC;
  319.     }
  320.   else if (bloc == last_bloc)
  321.     {
  322.       last_bloc = bloc->prev;
  323.       last_bloc->next = NIL_BLOC;
  324.     }
  325.   else if (bloc == first_bloc)
  326.     {
  327.       first_bloc = bloc->next;
  328.       first_bloc->prev = NIL_BLOC;
  329.     }
  330.   else
  331.     {
  332.       bloc->next->prev = bloc->prev;
  333.       bloc->prev->next = bloc->next;
  334.     }
  335.  
  336.   relocate_some_blocs (bloc->next, bloc->data);
  337.   relinquish (bloc->size);
  338.   free (bloc);
  339. }
  340.  
  341. /* Interface routines.  */
  342.  
  343. static int use_relocatable_buffers;
  344.  
  345. /* Obtain SIZE bytes of storage from the free pool, or the system, as
  346.    necessary.  If relocatable blocs are in use, this means relocating
  347.    them.  This function gets plugged into the GNU malloc's __morecore
  348.    hook.
  349.  
  350.    We provide hysteresis, never relocating by less than extra_bytes.
  351.  
  352.    If we're out of memory, we should return zero, to imitate the other
  353.    __morecore hook values - in particular, __default_morecore in the
  354.    GNU malloc package.  */
  355.  
  356. POINTER 
  357. r_alloc_sbrk (size)
  358.      long size;
  359. {
  360.   /* This is the first address not currently available for the heap.  */
  361.   POINTER top;
  362.   /* Amount of empty space below that.  */
  363.   /* It is not correct to use SIZE here, because that is usually unsigned.
  364.      ptrdiff_t would be okay, but is not always available.
  365.      `long' will work in all cases, in practice.  */
  366.   long already_available;
  367.   POINTER ptr;
  368.  
  369.   if (! use_relocatable_buffers)
  370.     return (*real_morecore) (size);
  371.  
  372.   top = first_bloc ? first_bloc->data : page_break_value;
  373.   already_available = (char *) top - (char *) virtual_break_value;
  374.  
  375.   /* Do we not have enough gap already?  */
  376.   if (size > 0 && already_available < size)
  377.     {
  378.       /* Get what we need, plus some extra so we can come here less often.  */
  379.       SIZE get = size - already_available + extra_bytes;
  380.  
  381.       if (! obtain (get))
  382.     return 0;
  383.  
  384.       if (first_bloc)
  385.     relocate_some_blocs (first_bloc, first_bloc->data + get);
  386.  
  387.       /* Zero out the space we just allocated, to help catch bugs
  388.      quickly.  */
  389.       bzero (virtual_break_value, get);
  390.     }
  391.   /* Can we keep extra_bytes of gap while freeing at least extra_bytes?  */
  392.   else if (size < 0 && already_available - size > 2 * extra_bytes)
  393.     {
  394.       /* Ok, do so.  This is how many to free.  */
  395.       SIZE give_back = already_available - size - extra_bytes;
  396.  
  397.       if (first_bloc)
  398.     relocate_some_blocs (first_bloc, first_bloc->data - give_back);
  399.       relinquish (give_back);
  400.     }
  401.  
  402.   ptr = virtual_break_value;
  403.   virtual_break_value += size;
  404.  
  405.   return ptr;
  406. }
  407.  
  408. /* Allocate a relocatable bloc of storage of size SIZE.  A pointer to
  409.    the data is returned in *PTR.  PTR is thus the address of some variable
  410.    which will use the data area.
  411.  
  412.    If we can't allocate the necessary memory, set *PTR to zero, and
  413.    return zero.  */
  414.  
  415. POINTER
  416. r_alloc (ptr, size)
  417.      POINTER *ptr;
  418.      SIZE size;
  419. {
  420.   register bloc_ptr new_bloc;
  421.  
  422.   if (! r_alloc_initialized)
  423.     r_alloc_init ();
  424.  
  425.   new_bloc = get_bloc (size);
  426.   if (new_bloc)
  427.     {
  428.       new_bloc->variable = ptr;
  429.       *ptr = new_bloc->data;
  430.     }
  431.   else
  432.     *ptr = 0;
  433.  
  434.   return *ptr;
  435. }
  436.  
  437. /* Free a bloc of relocatable storage whose data is pointed to by PTR.
  438.    Store 0 in *PTR to show there's no block allocated.  */
  439.  
  440. void
  441. r_alloc_free (ptr)
  442.      register POINTER *ptr;
  443. {
  444.   register bloc_ptr dead_bloc;
  445.  
  446.   dead_bloc = find_bloc (ptr);
  447.   if (dead_bloc == NIL_BLOC)
  448.     abort ();
  449.  
  450.   free_bloc (dead_bloc);
  451.   *ptr = 0;
  452. }
  453.  
  454. /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
  455.    Do this by shifting all blocks above this one up in memory, unless
  456.    SIZE is less than or equal to the current bloc size, in which case
  457.    do nothing.
  458.  
  459.    Change *PTR to reflect the new bloc, and return this value.
  460.  
  461.    If more memory cannot be allocated, then leave *PTR unchanged, and
  462.    return zero.  */
  463.  
  464. POINTER
  465. r_re_alloc (ptr, size)
  466.      POINTER *ptr;
  467.      SIZE size;
  468. {
  469.   register bloc_ptr bloc;
  470.  
  471.   bloc = find_bloc (ptr);
  472.   if (bloc == NIL_BLOC)
  473.     abort ();
  474.  
  475.   if (size <= bloc->size)
  476.     /* Wouldn't it be useful to actually resize the bloc here?  */
  477.     return *ptr;
  478.  
  479.   if (! obtain (size - bloc->size))
  480.     return 0;
  481.  
  482.   relocate_some_blocs (bloc->next, bloc->data + size);
  483.  
  484.   /* Zero out the new space in the bloc, to help catch bugs faster.  */
  485.   bzero (bloc->data + bloc->size, size - bloc->size);
  486.  
  487.   /* Indicate that this block has a new size.  */
  488.   bloc->size = size;
  489.  
  490.   return *ptr;
  491. }
  492.  
  493. /* The hook `malloc' uses for the function which gets more space
  494.    from the system.  */
  495. extern POINTER (*__morecore) ();
  496.  
  497. /* Initialize various things for memory allocation. */
  498.  
  499. static void
  500. r_alloc_init ()
  501. {
  502.   if (r_alloc_initialized)
  503.     return;
  504.  
  505.   r_alloc_initialized = 1;
  506.   real_morecore = __morecore;
  507.   __morecore = r_alloc_sbrk;
  508.  
  509.   virtual_break_value = break_value = (*real_morecore) (0);
  510.   if (break_value == NIL)
  511.     abort ();
  512.  
  513.   page_size = PAGE;
  514.   extra_bytes = ROUNDUP (50000);
  515.  
  516.   page_break_value = (POINTER) ROUNDUP (break_value);
  517.   /* Clear the rest of the last page; this memory is in our address space
  518.      even though it is after the sbrk value.  */
  519.   bzero (break_value, (page_break_value - break_value));
  520.   use_relocatable_buffers = 1;
  521. }
  522.